package Arrays101.Day04;

public class Day04_2 {
    public static void main(String[] args) {
        boolean flag = validMountainArray(new int[]{2,1,3,4,3,2});
        System.out.println(flag);
    }

    /**
     * Valid Mountain Array
     *
     * Given an array of integers arr, return true if and only if it is a valid mountain array.
     * Recall that arr is a mountain array if and only if:
     * arr.length >= 3
     * There exists some i with 0 < i < arr.length - 1
     * such that:
     * arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
     * arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
     * @param arr
     * @return
     */
    public static boolean validMountainArray(int[] arr) {
        if (arr.length<3){
            return false;
        }//1 2 3 4 5 4 3 2 1
        int len=0;
        for (int i = 1; i < arr.length -1; i++) {
                if (arr[i]>arr[i-1]&&arr[i]>arr[i+1]){
                    int left=i-1,right=i+1;
                    while (left>0&&arr[left]>arr[left-1]){
                        left--;
                    }
                    while (right<arr.length-1&&arr[right]>arr[right+1]){
                        right++;
                    }
                    len=right-left+1;
                }
        }
        return len==arr.length;
    }
}
